home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / vidhrdw / stfight.c < prev    next >
C/C++ Source or Header  |  2000-04-04  |  8KB  |  315 lines

  1. /***************************************************************************
  2.  
  3.   vidhrdw.c
  4.  
  5.   Functions to emulate the video hardware of the machine.
  6.  
  7. ***************************************************************************/
  8.  
  9. #include "driver.h"
  10.  
  11. // Real stuff
  12. unsigned char *stfight_text_char_ram;
  13. unsigned char *stfight_text_attr_ram;
  14. unsigned char *stfight_vh_latch_ram;
  15. unsigned char *stfight_sprite_ram;
  16.  
  17. static struct tilemap *fg_tilemap,*bg_tilemap,*tx_tilemap;
  18. static int flipscreen = 0;
  19. static int stfight_sprite_base = 0;
  20.  
  21. /*
  22.         Graphics ROM Format
  23.         ===================
  24.  
  25.         Each tile is 8x8 pixels
  26.         Each composite tile is 2x2 tiles, 16x16 pixels
  27.         Each screen is 32x32 composite tiles, 64x64 tiles, 256x256 pixels
  28.         Each layer is a 4-plane bitmap 8x16 screens, 2048x4096 pixels
  29.  
  30.         There are 4x256=1024 composite tiles defined for each layer
  31.  
  32.         Each layer is mapped using 2 bytes/composite tile
  33.         - one byte for the tile
  34.         - one byte for the tile bank, attribute
  35.             - b7,b5     tile bank (0-3)
  36.  
  37.         Each pixel is 4 bits = 16 colours.
  38.  
  39.  */
  40.  
  41. void stfight_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable,const unsigned char *color_prom)
  42. {
  43.     int i;
  44.     #define TOTAL_COLORS(gfxn) (Machine->gfx[gfxn]->total_colors * Machine->gfx[gfxn]->color_granularity)
  45.     #define COLOR(gfxn,offs) (colortable[Machine->drv->gfxdecodeinfo[gfxn].color_codes_start + offs])
  46.  
  47.  
  48.     /* unique color for transparency */
  49.     palette[256*3+0] = 0x04;
  50.     palette[256*3+1] = 0x04;
  51.     palette[256*3+2] = 0x04;
  52.  
  53.     /* text uses colors 192-207 */
  54.     for (i = 0;i < TOTAL_COLORS(0);i++)
  55.     {
  56.         if ((*color_prom & 0x0f) == 0x0f) COLOR(0,i) = 256;    /* transparent */
  57.         else COLOR(0,i) = (*color_prom & 0x0f) + 0xc0;
  58.         color_prom++;
  59.     }
  60.     color_prom += 256 - TOTAL_COLORS(0);    /* rest of the PROM is unused */
  61.  
  62.     /* fg uses colors 64-127 */
  63.     for (i = 0;i < TOTAL_COLORS(1);i++)
  64.     {
  65.         COLOR(1,i) = (color_prom[256] & 0x0f) + 16 * (color_prom[0] & 0x03) + 0x40;
  66.         color_prom++;
  67.     }
  68.     color_prom += 256;
  69.  
  70.     /* bg uses colors 0-63 */
  71.     for (i = 0;i < TOTAL_COLORS(2);i++)
  72.     {
  73.         COLOR(2,i) = (color_prom[256] & 0x0f) + 16 * (color_prom[0] & 0x03) + 0x00;
  74.         color_prom++;
  75.     }
  76.     color_prom += 256;
  77.  
  78.     /* sprites use colors 128-191 */
  79.     for (i = 0;i < TOTAL_COLORS(4);i++)
  80.     {
  81.         COLOR(4,i) = (color_prom[256] & 0x0f) + 16 * (color_prom[0] & 0x03) + 0x80;
  82.         color_prom++;
  83.     }
  84.     color_prom += 256;
  85. }
  86.  
  87. /***************************************************************************
  88.  
  89.   Callbacks for the TileMap code
  90.  
  91. ***************************************************************************/
  92.  
  93. static UINT32 fg_scan(UINT32 col,UINT32 row,UINT32 num_cols,UINT32 num_rows)
  94. {
  95.     /* logical (col,row) -> memory offset */
  96.     return (col & 0x0f) + ((row & 0x0f) << 4) + ((col & 0x70) << 4) + ((row & 0xf0) << 7);
  97. }
  98.  
  99. static void get_fg_tile_info(int tile_index)
  100. {
  101.     unsigned char   *fgMap = memory_region(REGION_GFX5);
  102.     int attr,tile_base;
  103.  
  104.     attr = fgMap[0x8000+tile_index];
  105.     tile_base = ((attr & 0x80) << 2) | ((attr & 0x20) << 3);
  106.  
  107.     SET_TILE_INFO(1,tile_base + fgMap[tile_index],attr & 0x07);
  108. }
  109.  
  110. static UINT32 bg_scan(UINT32 col,UINT32 row,UINT32 num_cols,UINT32 num_rows)
  111. {
  112.     /* logical (col,row) -> memory offset */
  113.     return ((col & 0x0e) >> 1) + ((row & 0x0f) << 3) + ((col & 0x70) << 3) +
  114.             ((row & 0x80) << 3) + ((row & 0x10) << 7) + ((col & 0x01) << 12) +
  115.             ((row & 0x60) << 8);
  116. }
  117.  
  118. static void get_bg_tile_info(int tile_index)
  119. {
  120.     unsigned char   *bgMap = memory_region(REGION_GFX6);
  121.     int attr,tile_bank,tile_base;
  122.  
  123.     attr = bgMap[0x8000+tile_index];
  124.     tile_bank = (attr & 0x20) >> 5;
  125.     tile_base = (attr & 0x80) << 1;
  126.  
  127.     SET_TILE_INFO(2+tile_bank,tile_base + bgMap[tile_index],attr & 0x07);
  128. }
  129.  
  130. static void get_tx_tile_info(int tile_index)
  131. {
  132.     unsigned char attr = stfight_text_attr_ram[tile_index];
  133.  
  134.     SET_TILE_INFO(0,stfight_text_char_ram[tile_index] + ((attr & 0x80) << 1),attr & 0x0f);
  135.     tile_info.flags = TILE_FLIPYX((attr & 0x60) >> 5);
  136. }
  137.  
  138.  
  139. /***************************************************************************
  140.  
  141.   Start the video hardware emulation.
  142.  
  143. ***************************************************************************/
  144.  
  145. int stfight_vh_start(void)
  146. {
  147.     bg_tilemap = tilemap_create(get_bg_tile_info,bg_scan,TILEMAP_OPAQUE,     16,16,128,256);
  148.     fg_tilemap = tilemap_create(get_fg_tile_info,fg_scan,TILEMAP_TRANSPARENT,16,16,128,256);
  149.     tx_tilemap = tilemap_create(get_tx_tile_info,tilemap_scan_rows,
  150.             TILEMAP_TRANSPARENT_COLOR,8,8,32,32);
  151.  
  152.     if (!fg_tilemap || !bg_tilemap || !tx_tilemap)
  153.         return 1;
  154.  
  155.     fg_tilemap->transparent_pen = 0x0F;
  156.     tx_tilemap->transparent_pen = 256;
  157.  
  158.     return 0;
  159. }
  160.  
  161.  
  162.  
  163. /***************************************************************************
  164.  
  165.   Memory handlers
  166.  
  167. ***************************************************************************/
  168.  
  169. WRITE_HANDLER( stfight_text_char_w )
  170. {
  171.     if (stfight_text_char_ram[offset] != data)
  172.     {
  173.         stfight_text_char_ram[offset] = data;
  174.         tilemap_mark_tile_dirty(tx_tilemap,offset);
  175.     }
  176. }
  177.  
  178. WRITE_HANDLER( stfight_text_attr_w )
  179. {
  180.     if (stfight_text_attr_ram[offset] != data)
  181.     {
  182.         stfight_text_attr_ram[offset] = data;
  183.         tilemap_mark_tile_dirty(tx_tilemap,offset);
  184.     }
  185. }
  186.  
  187. WRITE_HANDLER( stfight_sprite_bank_w )
  188. {
  189.     stfight_sprite_base = ( ( data & 0x04 ) << 7 ) |
  190.                           ( ( data & 0x01 ) << 8 );
  191. }
  192.  
  193. WRITE_HANDLER( stfight_vh_latch_w )
  194. {
  195.     int scroll;
  196.  
  197.  
  198.     stfight_vh_latch_ram[offset] = data;
  199.  
  200.     switch( offset )
  201.     {
  202.         case 0x00:
  203.         case 0x01:
  204.             scroll = (stfight_vh_latch_ram[1] << 8) | stfight_vh_latch_ram[0];
  205.             tilemap_set_scrollx(fg_tilemap,0,scroll);
  206.             break;
  207.  
  208.         case 0x02:
  209.         case 0x03:
  210.             scroll = (stfight_vh_latch_ram[3] << 8) | stfight_vh_latch_ram[2];
  211.             tilemap_set_scrolly(fg_tilemap,0,scroll);
  212.             break;
  213.  
  214.         case 0x04:
  215.         case 0x05:
  216.             scroll = (stfight_vh_latch_ram[5] << 8) | stfight_vh_latch_ram[4];
  217.             tilemap_set_scrollx(bg_tilemap,0,scroll);
  218.             break;
  219.  
  220.         case 0x06:
  221.         case 0x08:
  222.             scroll = (stfight_vh_latch_ram[8] << 8) | stfight_vh_latch_ram[6];
  223.             tilemap_set_scrolly(bg_tilemap,0,scroll);
  224.             break;
  225.  
  226.         case 0x07:
  227.             tilemap_set_enable(tx_tilemap,data & 0x80);
  228.             /* 0x40 = sprites */
  229.             tilemap_set_enable(bg_tilemap,data & 0x20);
  230.             tilemap_set_enable(fg_tilemap,data & 0x10);
  231.             flipscreen = data & 0x01;
  232.             tilemap_set_flip(ALL_TILEMAPS,flipscreen ? (TILEMAP_FLIPY | TILEMAP_FLIPX) : 0);
  233.             break;
  234.     }
  235. }
  236.  
  237. /***************************************************************************
  238.  
  239.   Display refresh
  240.  
  241. ***************************************************************************/
  242.  
  243. static void draw_sprites(struct osd_bitmap *bitmap)
  244. {
  245.     int offs,sx,sy;
  246.  
  247.     for (offs = 0;offs < 4096;offs += 32)
  248.     {
  249.         int code;
  250.         int attr = stfight_sprite_ram[offs+1];
  251.         int flipx = attr & 0x10;
  252.         int color = attr & 0x0f;
  253.         int pri = (attr & 0x20) >> 5;
  254.  
  255.         sy = stfight_sprite_ram[offs+2];
  256.         sx = stfight_sprite_ram[offs+3];
  257.  
  258.         // non-active sprites have zero y coordinate value
  259.         if( sy > 0 )
  260.         {
  261.             // sprites which wrap onto/off the screen have
  262.             // a sign extension bit in the sprite attribute
  263.             if( sx >= 0xf0 )
  264.             {
  265.                 if (attr & 0x80)
  266.                     sx -= 0x100;
  267.             }
  268.  
  269.             if (flipscreen)
  270.             {
  271.                 sx = 240 - sx;
  272.                 sy = 240 - sy;
  273.                 flipx = !flipx;
  274.             }
  275.  
  276.             code = stfight_sprite_base + stfight_sprite_ram[offs];
  277.  
  278.             pdrawgfx(bitmap,Machine->gfx[4],
  279.                      code,
  280.                      color,
  281.                      flipx,flipscreen,
  282.                      sx,sy,
  283.                      &Machine->drv->visible_area,TRANSPARENCY_PEN,0x0f,
  284.                      pri ? 0x02 : 0);
  285.         }
  286.     }
  287. }
  288.  
  289.  
  290. void stfight_vh_screenrefresh( struct osd_bitmap *bitmap,int full_refresh )
  291. {
  292.     tilemap_update(ALL_TILEMAPS);
  293.  
  294.     if (palette_recalc())
  295.         tilemap_mark_all_pixels_dirty(ALL_TILEMAPS);
  296.  
  297.     tilemap_render(ALL_TILEMAPS);
  298.  
  299.  
  300.     fillbitmap(priority_bitmap,0,NULL);
  301.  
  302.     if (bg_tilemap->enable)
  303.         tilemap_draw(bitmap,bg_tilemap,0);
  304.     else
  305.         fillbitmap(bitmap,Machine->pens[0],&Machine->drv->visible_area);
  306.  
  307.     tilemap_draw(bitmap,fg_tilemap,1<<16);
  308.  
  309.     /* Draw sprites (may be obscured by foreground layer) */
  310.     if (stfight_vh_latch_ram[0x07] & 0x40)
  311.         draw_sprites(bitmap);
  312.  
  313.     tilemap_draw(bitmap,tx_tilemap,0);
  314. }
  315.